﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApAPPTranslationLimitWall
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

public class CApAPPTranslationLimitWall
{
  private KiAppTranslationLimitWallEx m_translationLimitWallEx;
  private CApAPPTranslationLimitWallManipulator m_wallManipulator;
  private string m_wallName;
  private bool m_isWallDisplayActivated;

  public event WallLimitsDidChangedEventHandler WallLimitsDidChanged;

  public CApAPPTranslationLimitWall(
    CApAPPLimitsViewModel limitsViewModel,
    KiAppTranslationLimitWallAxis axis,
    string name)
  {
    this.Axis = axis;
    this.m_wallManipulator = (CApAPPTranslationLimitWallManipulator) null;
    this.m_wallName = name;
    this.LimitsViewModel = limitsViewModel;
    this.CreateTranslationLimitWall();
  }

  private CApAPPLimitsViewModel LimitsViewModel { get; set; }

  public bool IsWallDisplayActivated
  {
    get => this.m_isWallDisplayActivated;
    set
    {
      this.m_isWallDisplayActivated = value;
      this.UpdateWallDisplay();
    }
  }

  private void CreateTranslationLimitWall()
  {
    if (this.m_translationLimitWallEx != null)
      this.m_translationLimitWallEx.Deactivate();
    this.m_translationLimitWallEx = new KiAppTranslationLimitWallEx(this.LimitsViewModel.ReferenceFrame, this.Axis, this.LimitsViewModel.Limits, this.m_wallName);
    if (this.m_translationLimitWallEx.WallObject == null)
      return;
    new TxObjectTreeInternal().ExcludeNode(this.m_translationLimitWallEx.WallObject);
  }

  public KiAppTranslationLimitWallAxis Axis { get; set; }

  public ITxObject WallObject
  {
    get => this.m_translationLimitWallEx.WallObject;
    set
    {
    }
  }

  public void Activate()
  {
    if (this.m_translationLimitWallEx == null)
      return;
    this.m_translationLimitWallEx.Activate();
    if (this.m_translationLimitWallEx.WallObject == null)
      return;
    new TxObjectTreeInternal().ExcludeNode(this.m_translationLimitWallEx.WallObject);
  }

  public void Deactivate()
  {
    if (this.m_translationLimitWallEx.IsActivate())
      this.m_translationLimitWallEx.Deactivate();
    this.DeactivateManipulator();
  }

  public bool IsActivated()
  {
    return this.m_translationLimitWallEx != null && this.m_translationLimitWallEx.IsActivate();
  }

  public void ClearData() => this.Deactivate();

  public void UpdateWallDisplay()
  {
    this.DoUpdateWallDisplay();
    this.DoUpdateManipulatorState();
  }

  private void DoUpdateWallDisplay()
  {
    if (this.m_translationLimitWallEx == null)
      return;
    if (this.ShouldWallBeDisplayed())
    {
      if (!this.IsActivated())
        this.Activate();
      this.m_translationLimitWallEx.ReferenceFrame = this.LimitsViewModel.ReferenceFrame;
      this.m_translationLimitWallEx.UpdateTranslationLimitWall();
    }
    else
      this.Deactivate();
  }

  private void DoUpdateManipulatorState()
  {
    if (this.ShouldManipulatorBeActivated())
    {
      if (this.IsManipulatorActivated())
        return;
      this.ActivateManipulator();
    }
    else
      this.DeactivateManipulator();
  }

  private bool ShouldManipulatorBeActivated()
  {
    bool flag = false;
    if (this.LimitsViewModel.Limits != null)
    {
      switch ((int) this.Axis)
      {
        case 0:
        case 1:
          flag = this.LimitsViewModel.Limits.TxDofStatus == 1;
          break;
        case 2:
        case 3:
          flag = this.LimitsViewModel.Limits.TyDofStatus == 1;
          break;
        case 4:
        case 5:
          flag = this.LimitsViewModel.Limits.TzDofStatus == 1;
          break;
      }
    }
    return flag && this.m_isWallDisplayActivated;
  }

  private bool IsManipulatorActivated()
  {
    return this.m_wallManipulator != null && this.m_wallManipulator.IsActivated;
  }

  private void ActivateManipulator()
  {
    if (this.m_wallManipulator == null)
      this.m_wallManipulator = new CApAPPTranslationLimitWallManipulator(this.LimitsViewModel, this);
    else
      this.m_wallManipulator.Activate();
  }

  private void DeactivateManipulator()
  {
    if (this.m_wallManipulator == null)
      return;
    this.m_wallManipulator.Release();
    this.m_wallManipulator = (CApAPPTranslationLimitWallManipulator) null;
  }

  private bool ShouldWallBeDisplayed()
  {
    bool flag = false;
    if (this.LimitsViewModel.Limits != null)
    {
      switch ((int) this.Axis)
      {
        case 0:
        case 1:
          flag = this.LimitsViewModel.Limits.TxDofStatus > 0;
          break;
        case 2:
        case 3:
          flag = this.LimitsViewModel.Limits.TyDofStatus > 0;
          break;
        case 4:
        case 5:
          flag = this.LimitsViewModel.Limits.TzDofStatus > 0;
          break;
      }
    }
    return flag && this.m_isWallDisplayActivated;
  }

  public void LimitsDidChanged()
  {
    if (this.WallLimitsDidChanged == null)
      return;
    this.WallLimitsDidChanged((object) this, new EventArgs());
  }
}
